package cn.kuporo.seckill.order.controller;

import cn.kuporo.seckill.order.application.model.command.SeckillOrderCommand;
import cn.kuporo.seckill.common.model.dto.SeckillOrderSubmitDTO;
import cn.kuporo.seckill.common.model.response.Response;
import cn.kuporo.seckill.order.application.model.command.SeckillOrderTaskCommand;
import cn.kuporo.seckill.order.application.service.SeckillOrderService;
import cn.kuporo.seckill.order.application.service.SeckillSubmitOrderService;
import cn.kuporo.seckill.order.domain.model.entity.SeckillOrder;
import com.alibaba.nacos.api.model.v2.ErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: kuporo
 * @since: 2025/7/7
 * @description:
 */
@RestController
@RequestMapping("/order")
@RequiredArgsConstructor
public class SeckillOrderController {
    private final SeckillOrderService seckillOrderService;
    private final SeckillSubmitOrderService seckillSubmitOrderService;

    /**
     * 保存秒杀订单
     */
    @PostMapping( "/save")
    public Response<SeckillOrderSubmitDTO> saveSeckillOrder(@RequestBody @Validated SeckillOrderCommand seckillOrderCommand){
        return Response.success(seckillSubmitOrderService.saveSeckillOrder(1L, seckillOrderCommand));
    }
    
    /**
     * 获取用户维度的订单列表
     */
    @GetMapping( "/getByUserId")
    public Response<List<SeckillOrder>> getSeckillOrderByUserId(Long userId){
        return Response.success(seckillOrderService.getSeckillOrderByUserId(userId));
    }


    /**
     * 异步下单时，会返回任务id，如果下单返回的是任务id，则调用此接口获取订单id，如果前端多次调用此接口返回的数据仍为为任务id，则表示下单失败，不再调用
     * 如果调用此接口（含重试），返回了订单id，则异步下单获取到了与同步下单相同的数据，就可以通过订单id来查询订单信息了。
     */
    @GetMapping("/getSeckillOrderSubmitDTO")
    public Response<SeckillOrderSubmitDTO> get(SeckillOrderTaskCommand seckillOrderTaskCommand){
        SeckillOrderSubmitDTO seckillOrderSubmitDTO = seckillOrderService.getSeckillOrderSubmitDTOByTaskId(seckillOrderTaskCommand);
        return Response.success(seckillOrderSubmitDTO);
    }

}
